﻿namespace AzureLogProviders.Common
{
    using System;

    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Fluent;
    using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;

    public static class LogConfigurationSourceBuilderExtensions
    {
        public static ILoggingConfigurationStart ConfigureLogging(this IConfigurationSourceBuilder configurationSourceBuilder, bool overrideSttings, out bool alreadyConfigured)
        {
            alreadyConfigured = false;
            if (configurationSourceBuilder == null)
            {
                throw new ArgumentNullException("configurationSourceBuilder");
            }
            LoggingSettings loggingSettings = null;
            if (!overrideSttings)
            {
                loggingSettings =
                    configurationSourceBuilder.Get(LoggingSettings.SectionName) as LoggingSettings;
                if (loggingSettings == null)
                {
                    loggingSettings = new LoggingSettings();
                    configurationSourceBuilder.AddSection(LoggingSettings.SectionName, loggingSettings);
                }
                else
                {
                    alreadyConfigured = true;
                }
            }
            else
            {
                loggingSettings = new LoggingSettings();
            }

            return new LoggingConfigurationBuilder(loggingSettings);
        }

    }
}
